package rd.hardcore.engine.sample.source {
	import rd.hardcore.events.SampleSourceEvent;
	import rd.hardcore.engine.sample.Sample;
	import rd.hardcore.engine.sample.SampleDatas;

	import flash.events.EventDispatcher;
	import flash.filesystem.File;

	/**
	 * 
	 * @see SampleSourceEvent#EXTRACTED
	 */[Event( name = "_sse_EXTRACTED", type = "SampleSourceEvent" )]

	/**
	 * 
	 * @see SampleSourceEvent#ENCODED
	 */[Event( name = "_sse_ENCODED", type = "SampleSourceEvent" )]
	
	/**
	 * @author plepers
	 */
	public class SampleSource extends EventDispatcher {

		public function SampleSource ( file : File ) {
			_sampleDatas = new SampleDatas();
			_file = file;
		}

		public function getDatas () : SampleDatas {
			return _sampleDatas;
		}

		
		public function extract () : void {
			setExtracted( false );
		}

		public function encode () : void {
			
		}
		
		public function get extracted ( ) : Boolean {
			return _extracted;
		}
				
		protected function setExtracted ( flag :  Boolean ) : void {
			if( _extracted == flag) return;
			_extracted = flag;
			if( _extracted )
				dispatchEvent( new SampleSourceEvent( SampleSourceEvent.EXTRACTED ) );
		}



		private var _extracted : Boolean = false;
				
		
		protected var _file : File;
		
		protected var _sampleDatas : SampleDatas;

		
		//_____________________________________________________________________________
		//																		 STATIC
		
		public static const WAV : String = "wav";
		public static const MP3 : String = "mp3";

		public static function getSampleSource ( file : File ) : SampleSource {
			
			switch( file.extension.toLowerCase() ){
				case WAV:
					return new WavSource( file );
					break;
				case MP3:
					return new MP3Source( file );
					break;
				default:
			}
			
			throw new ArgumentError( "SampleSource.getSampleSource() : invalid file "+ file.nativePath );
			
			return null;
		}

		
		public static function isSupportedSound ( file : File ) : Boolean {
			return _supportedFormat.indexOf( file.extension.toLowerCase() ) > -1;
		}
		
		private static const _supportedFormat : Vector.<String> = _initSF( );

		private static function _initSF () : Vector.<String> {
			var sf : Vector.<String> = new Vector.<String>(2, true);
			sf[0] = WAV;
			sf[1] = MP3;
			return sf;
		}

		public function getSample () : Sample {
			return new Sample( this );
		}
	}
}
